package com.wapper;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;

public class test01 {

    public static void main(String[] args) {


        book b1[]=new book[3];
        b1[0]=new book("红花",12.3);
        b1[1]=new book("风",299.2);
        b1[2]=new book("超级笨蛋",2.2);
        //按照书名排序
//        Arrays.sort(b1, new Comparator() {
//            @Override
//            public int compare(Object o1, Object o2) {
//              book b= (book)o1;
//              book b1=(book)o2;
//              return b.getName().length()-b1.getName().length();
//            }
//
//
//        });
        //按照价格排序
        Arrays.sort(b1, new Comparator() {

            @Override
            public int compare(Object o1, Object o2) {

                book b= (book)o1;
                book b1=(book)o2;

               if(b.getPrice()-b1.getPrice()>0){
                   return 1;
               }
               else if(b.getPrice()-b1.getPrice()<0){

                   return -1;

               }
               else{
                   return 0;
               }
            }
        });

        System.out.println(Arrays.toString(b1));
    }
}
class book{
    private String name;
    private  double price;

    public book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}